import numpy as np
from matplotlib import pyplot as plt

num = 40
xi = np.linspace(1, 10, num)
yi = -4 * xi + 4 + np.random.uniform(-3, 3, num)
plt.figure()
plt.scatter(xi, yi)

X = np.array([[1 for i in range(num)], xi]).T
Y = np.array([yi]).T
t = np.dot(X.T, X)
t = np.linalg.inv(t)
t = np.dot(t, X.T)
res = np.dot(t, Y)
print(res)

w = res[1][0]
b = res[0][0]

plt_x = np.linspace(0, 10, 20)
plt_y = w * plt_x + b
plt.plot(plt_x, plt_y, color='r')

plt.show()